package com.lxr0860.lxr0860production_test.itemactivity;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.Window;
import android.widget.TextView;


import com.lxr0860.lxr0860production_test.R;
import com.lxr0860.lxr0860production_test.utils.Contants;
import com.lxr0860.lxr0860production_test.view.CustomDialog;
import com.lxr0860.lxr0860production_test.view.HorizontalProgressBar;

import org.jetbrains.annotations.Nullable;


/**
 * Created by uniriho on 2018/9/3.
 */

public class LightSensorActivity extends Activity {
    private TextView mTvSensorNumber;
    private HorizontalProgressBar mProgressBar;
    private SensorManager mSensorManager;
    private Sensor mSensor;
    private SensorEventListener mListener;
    //    private static final int MAX_LIGHT = 65535;
    private static final int MAX_LIGHT = 600;
    private static final int MIN_LIGHT = 300;
    private boolean max, min;
    private CustomDialog.Builder builder;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Contants.DIALOG_POSITIVE:
                    Intent mIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("result", true);
                    mIntent.putExtras(bundle);
                    LightSensorActivity.this.setResult(Contants.TEST_ITEM_LIGHT_SENSOR, mIntent);
                    finish();
                    break;
                case Contants.DIALOG_NEGATIVE:
                    builder = null;
                    max = min = false;
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_light_sensor);
        initView();
        initData();
    }

    private void initView() {
        mTvSensorNumber = (TextView) findViewById(R.id.tv_light_sensor_number);
        mProgressBar = findViewById(R.id.pb_light_sensor);

    }

    private void initData() {
        mProgressBar.setMax(MAX_LIGHT);
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
        mListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent event) {
                float value = event.values[0];
                if (value >= MAX_LIGHT) {
                    max = true;
                }
                if (value < MIN_LIGHT) {
                    min = true;
                }
                mProgressBar.setProgress((int) value);
                mTvSensorNumber.setText("当前光线值：" + value + " lux");
                if (max && min) {
                    if (builder == null) {
                        builder = Contants.showDailog(
                                LightSensorActivity.this, "光感正常，是否保存测试", handler, Gravity.TOP);
                    }
                }
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {

            }
        };
        mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_FASTEST);
    }

    protected void onDestroy() {
        super.onDestroy();
        //注销监听器
        if (mListener != null) {
            mSensorManager.unregisterListener(mListener);
        }
    }
}
